﻿(function () {
	$(function () {
		$('#TotalLevel').blur(function () {
			if (this.attributes['oldval'].value != this.value) {
				this.attributes['oldval'].value = this.value;
				$('.levelPercentItems').html(renderLevelPercentItem(this.value));
			}
		});
		$('#sumbitBut').click(function () {
			if (validateParam()) {
				$('form').submit();
			}
		});
	});

	function validateParam() {
		if ($('#TotalLevel').val() > 9) {
			alert('分销级别超过9级');
			return false;
		}
		{
			var percent = 0;
			$('.lp-item').each(function () {
				percent += parseFloat(this.value);
			});
			if (percent > 100) {
				alert('各级占比超过100%');
				return false;
			}
		}
		{
			var val = parseFloat($('#ProductPumpedValue').val());
			if (isNaN(val) || val < 0) {
				alert('抽成值必须大于0');
				return false;
			}
			switch ($('#ProductPumpedType').val()) {
				case 'Percentage':
					if (val > 100) {
						alert('按百分比抽成时不能大于100%');
						return false;
					}
					break;
			}
		}
		return true;
	}

	function renderLevelPercentItem(level) {
		var elStr = '';
		for (var i = 1; i <= level; i++) {
			elStr += levelPercentTemp.replace(/#level/g, i)
				.replace(/#index/g, i - 1)
				.replace(/#level/g, i)
				.replace(/#percent/g, '');
		}
		return elStr;
	}

	var levelPercentTemp = '<tr>\
					<td>#level级</td>\
					<td>\
						<input name="LevelPercent[#index].Level" id="LevelPercent_#index__Level" type="hidden" value="#level">\
						<input class="lp-item" name="LevelPercent[#index].Percent" id="LevelPercent_#index__Percent" type="text" value="#percent">%\
					</td>\
				</tr>';
})();